import { defineStore } from 'pinia';
import { commonAddress, commonConfigCli, commonConfigRank } from '@/api/common';
import type { CommonConfigCliResult } from '@/api/common/types';

type ConfigCli = Record<string, CommonConfigCliResult[number]['value']>;
const useConfigStore = defineStore('config', {
  state: () => ({
    /* 配置列表-客户端 */
    configCli: {} as ConfigCli,
    /* 配置-省市区 */
    address: [] as Array<any>,
    /* 配置搜索排行 */
    configRank: {},
  }),
  getters: {},
  actions: {
    async setConfigCli() {
      const result = await commonConfigCli();
      const configCli = result.reduce((acc, { field, remark, value }) => {
        acc[`${field}$$${remark}`] = value;
        return acc;
      }, {} as ConfigCli);
      this.configCli = configCli;
    },
    async setAddress() {
      const address = await commonAddress();
      this.address = address;
    },
    async setConfigRank() {
      const configRank = await commonConfigRank();
      this.configRank = configRank;
    },
  },
  persist: {
    enabled: true,
  },
});

export default useConfigStore;
